<?php
 /**
  *  @package ARXLib
  *  @version 2
  *  @subpackage Inputs
  *
  *  @copyright Ringo-Web Inc. 2004 - 2005
  */
//////////////////////////////////////////////////////////////////////////////////////
// CARXSelect
//////////////////////////////////////////////////////////////////////////////////////

    function GetSelectTreeItem($row, $level, $arxSelectCtrl)
    {
        $value = htmlspecialchars($row[$arxSelectCtrl->m_dbIDFieldName]);
        $label = htmlspecialchars($row[$arxSelectCtrl->m_dbLabelFieldName]);
        $fill = str_repeat("&nbsp; ", $level*2);
        $selected = "";
        if ($arxSelectCtrl->m_value === $value)
        {
            $selected = " selected";
        }
        return "<option value=\"" . $value . "\"$selected>" . $fill . $label . "</option>\n";
    }

    class CARXSelect extends CARXInput
    {
        /**
         * @var CARXTable
         */
        var $m_dbLookupTable;
        /**
         * @var CARXTable
         */
        var $m_dbTargetTable;
        var $m_dbIDFieldName;
        var $m_dbTargetIDFieldName;
        var $m_dbLabelFieldName;
        var $m_dbTreeParentFieldName;
        var $m_zeroOption;
        var $m_root;

        // $dbLookupTable could be array of options, e.g.:
        // array("0" => "tea", "1" => "coffee");
        function CARXSelect($label, $dbFieldName, &$dbLookupTable, $dbIDFieldName, $dbLabelFieldName,
            $dbTreeParentFieldName = "", $zeroOption = NULL, $addtag = "", $dbTargetTable = NULL, $dbTargetIDFieldName = NULL, $root = 0)
        {
            parent::CARXInput($label, $dbFieldName);
            $this->m_dbLookupTable =& $dbLookupTable;
            $this->m_dbTargetTable =& $dbTargetTable;
            $this->m_dbIDFieldName = $dbIDFieldName;
            $this->m_dbTargetIDFieldName = $dbTargetIDFieldName;
            $this->m_dbLabelFieldName = $dbLabelFieldName;
            $this->m_dbTreeParentFieldName = $dbTreeParentFieldName;
            $this->m_zeroOption = $zeroOption;
            $this->m_TagParams = $addtag;
            $this->m_root = $root;
        }

        function GetHTML()
        {
   
            $name = $this->GetHTMLCtrlName();

            $roresult = "";
            $result .= "<select name=\"$name\"".($this->m_TagParams?" ".$this->m_TagParams:"").">\n";
            if (!($this->m_zeroOption === NULL))
            {
                $charset = GetCharset();
                $zeroTxt = htmlentities($this->m_zeroOption, ENT_COMPAT, $charset);
                $result .= "<option value=\"0\">" . $zeroTxt . "</option>\n";
            }

            if (is_array($this->m_dbLookupTable))
            {
                $optArray = $this->m_dbLookupTable;
                foreach ($optArray as $key => $val)
                {
                    $value = htmlspecialchars($key);
                    $label = htmlspecialchars($val);
                    $selected = "";
                    if (isset($this->m_dbTargetTable))
                    {
                        $getKeyTable = $this->GetDBTable();
                        $getKeyTable->Open();
                        $keyID = $getKeyTable->GetField($this->m_dbTargetIDFieldName);
                        $getKeyTable->Close();
                        if ($keyID)
                        {
                            $this->m_dbTargetTable->SetFilter($this->m_dbIDFieldName."=".$value." and ".$this->m_dbTargetIDFieldName."=".$keyID);
                            $this->m_dbTargetTable->Open();
                            if ($this->m_dbTargetTable->GetRowCount())
                            {
                                $selected = " selected";
                                $roresult = "$label";
                            }
                        }
                    }
                    elseif ($this->m_value == $value)
                    {
                        $selected = " selected";
                        $roresult = "$label";
                    }
                    $result .= "<option value=\"" . $value . "\"$selected>" . $label . "</option>\n";
                }
            }
            else
            {
                if ($this->m_dbTreeParentFieldName)
                {
                    $keyField = $this->m_dbIDFieldName;
                    $parentField = $this->m_dbTreeParentFieldName;
                    $result .= GetDBTree($this->m_dbLookupTable, $keyField, $parentField, "GetSelectTreeItem", $this->m_root, 0, $this, $this->m_dbLookupTable->m_filter);
                }
                else
                {
                    $keyField = $this->m_dbIDFieldName;
                    $this->m_dbLookupTable->Open();
                    while ($row = $this->m_dbLookupTable->GetRow())
                    {
                        $value = $row[$this->m_dbIDFieldName];
                        $label = $row[$this->m_dbLabelFieldName];
                        $selected = "";
                        if (isset($this->m_dbTargetTable))
                        {
                            $getKeyTable = $this->GetDBTable();
                            $getKeyTable->Open();
                            $keyID = $getKeyTable->GetField($this->m_dbTargetIDFieldName);
                            $getKeyTable->Close();
                            if ($keyID)
                            {
                                $this->m_dbTargetTable->SetFilter($this->m_dbIDFieldName."=".$value." and ".$this->m_dbTargetIDFieldName."=".$keyID);
                                $this->m_dbTargetTable->Open();
                                if ($this->m_dbTargetTable->GetRowCount())
                                {
                                    $selected = " selected";
                                    $roresult = "$label";
                                }
                            }
                        }else
                        if ($this->m_value == $value)
                        {
                            $selected = " selected";
                            $roresult = "$label";
                        }
                        $result .= "<option value=\"" . $value . "\"$selected>" . $label . "</option>\n";
                        $this->m_dbLookupTable->Next();
                    }
                    $this->m_dbLookupTable->Close();
                }
            }

            $result .= "</select>\n";

            if (!$this->m_dbConnected)
            {
                return $roresult;
            }
            else
            {
                return $result;
            }
        }
    }


?>